package com.spzx.report.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.report.entity.VOrderInfo;
import com.spzx.report.service.SpzxAiReportService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SpzxAiReportController extends BaseController {

    @Autowired
    SpzxAiReportService spzxAiReportService;

    @Operation(summary ="获取Ai报表")
    @GetMapping("/getAiReport/{question}")
    public AjaxResult getAiReport(@PathVariable("question") String question) {

        Map<String, String> dataMap = spzxAiReportService.getAiReport(question);

        return success(dataMap);
    }

    @Operation(summary ="获取Ai报表")
    @GetMapping("/getDataAiReport/{dataKey}")
    public AjaxResult getDataAiReport(@PathVariable("dataKey") String dataKey) {
        Map<String, Object> xyMap = new HashMap<>();
        List<Object> xList = new ArrayList<>();
        List<Object> yList = new ArrayList<>();

        //调用Ai接口获取数据
        List<Map<String, Object>> reportMapList = spzxAiReportService.getDataAiReportFromDB(dataKey);

        //数据分组填充,横纵坐标
        for (Map<String, Object> reportMap : reportMapList) {
            String groups = reportMap.get("groupTag").toString();
            Object count = reportMap.get("count").toString();
            xList.add(groups);
            yList.add(count);
        }
        xyMap.put("xList",xList);
        xyMap.put("yList",yList);

        return success(xyMap);
    }

    @Operation(summary = "获取所有报告内容")
    @GetMapping("/getAllReport")
    public AjaxResult getAllReport() {
        List<VOrderInfo> orderInfoList = spzxAiReportService.list();
        return success(orderInfoList);
    }

    @GetMapping("/testAi")
    public String testAi() {
        String answer = spzxAiReportService.testAi();
        return answer;
    }
}
